﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    class Search
    {
        private List<Packages> _packages = new List<Packages>(new DbAccess().SelectPackages());
        private List<Products> _products = new List<Products>(new DbAccess().SelectProducts());
        private List<Suppliers> _suppliers = new List<Suppliers>(new DbAccess().SelectSuppliers());
        private List<ProductsSuppliers> _prodSupList = new List<ProductsSuppliers>(new DbAccess().SelectProductsSuppliers());
        private List<PackagesProductsSupplier> _pkgProdList = new List<PackagesProductsSupplier>(new DbAccess().SelectPackagesProSupp());

        private int _supId;
        private int _proId;
        private bool _supAll = false;
        private bool _proAll = false;

        public List<ProductsSuppliers> filterProSup(Form form)
        {


            //get the values in the search filter comboboxes
            //get the control
            Control[] supCtrl = form.Controls.Find("cmbSearchSuppliers", true);
            //get the id from the selecteditem
            if (supCtrl[0].GetType() == typeof(Suppliers))
                { _supId = ((Suppliers)((ComboBox)supCtrl[0]).SelectedItem).SupplierId; }
            else { _supAll = true; }

            Control[] proCtrl = form.Controls.Find("cmbSearchProducts", true);
            if (proCtrl[0].GetType() == typeof(Products))
                { _proId = ((Products)((ComboBox)proCtrl[0]).SelectedItem).ProductId; }
            else { _proAll = true; }


            if (!_supAll && !_proAll)
            {
                //return productsupplier list filtered on productid and supplierid
                List<ProductsSuppliers> tmpRes = new List<ProductsSuppliers>();
                List<ProductsSuppliers> results = new List<ProductsSuppliers>();

                foreach (ProductsSuppliers entry in _prodSupList)
                    { if (entry.ProductId == _proId) { tmpRes.Add(entry); } }

                foreach (ProductsSuppliers entry in tmpRes)
                    { if (entry.SupplierId == _supId) { results.Add(entry); } }

                return results;

            }

            else if (_supAll && !_proAll)
            {
                //return productssupplier list filtered on productid
                
                List<ProductsSuppliers> results = new List<ProductsSuppliers>();
                
                foreach (ProductsSuppliers entry in _prodSupList)
                    { if (entry.ProductId == _proId) { results.Add(entry); } }

                return results;

            }

            else if (!_supAll && _proAll)
            {
                //return productsupplier list filtered on supplierid

                List<ProductsSuppliers> results = new List<ProductsSuppliers>();

                foreach (ProductsSuppliers entry in _prodSupList)
                    { if (entry.SupplierId == _supId) { results.Add(entry); } }

                return results;

            }

            
            else { return _prodSupList; } //none or invalid or inconceivable selection made for filtering :D

        }


    }
}
